<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sender Chat Example</title>
    <script src="./jquery-1.12.1.js"></script>
    <script src="./socket.io.js"></script>
    <script>
        $(document).ready(function(){
			$.get({
				url: "http://59.110.5.217:30014/splitter/enzyme",
				success: function (data) {
					console.log(data);
				
					if(data.code != 0){
						console.log(data);
						return;
					}
					
					var wsUrl = "ws://59.110.5.217:30015/msg"//data.data;
					console.log(wsUrl);
					
					var ws = new WebSocket(wsUrl);
					ws.onopen = WSonOpen;
					ws.onmessage = WSonMessage;
					ws.onclose = WSonClose;
					ws.onerror = WSonError;

					function WSonOpen() {
						Log("连接已经建立。", "OK");
						$("#message").append("<h4>初始化：</h4>");
					};

					function WSonMessage(event) {
						$("#message").append("<h4>来自服务端消息："+event.data+"</h4>");
						Log(event.data);
					};

					function WSonClose() {
						Log("连接关闭。", "ERROR");
					};


					function WSonError() {
						Log("WebSocket错误。", "ERROR");
					};

					function Log(mes){
						console.log(mes);
					}
					$("#send").click(function(){
						var command = {
							"command":"sendmessage",
							"version":"1.0",
							"id":"b5265010-bb7a-11e6-a60f-77d95c87b19f",
							"data" :{	
								"type":"message",
								"sender":"test",
								"accepter":$('#accepter').val(),
								"content":$('#content').val(),
								"options":{
									"save":"true"
								}
							}
						}
						console.log(command);
						ws.send(JSON.stringify(command));
					});
			
			},
				error:function() {
					return alert("系统异常,获取用户信息失败");
				}
			});
        });
    </script>
</head>
<body>
<div>
    <div id="message">

    </div>
	接收人：<input id="accepter" name="accepter">
    消息内容：<input id="content" name="content">
    <button id="send">发送</button>
</div>
</body>
</html>